package com.huiquan.framework.base;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author lichao email:lichao@witspring.com
 * @Description: BaseController
 * @Date 2018/5/31 16:36
 * @since Ver V0.0.1
 */
public class BaseController {

    /**
     * 从request中取值
     *
     * @param name
     * @return
     */
    protected String getRequestValue(String name) {
        return getRequest().getParameter(name);
    }

    /**
     * 设置session的值
     *
     * @param name
     * @param value
     */
    protected void setSessionValue(String name, Object value) {
        getSession().setAttribute(name, value);
    }

    /**
     * 从session取值
     *
     * @param name
     * @return
     */
    protected Object getSessionValue(String name) {
        return getSession().getAttribute(name);
    }

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    protected HttpSession getSession() {
        return getRequest().getSession();
    }

    protected HttpServletResponse getResponse() {
        return ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
    }
}
